package so.shanku.essential.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseActivity;
import so.shanku.essential.adapter.ListViewAdapter;
import so.shanku.essential.adapter.ListViewAdapter.OnMyCheckChangedListener;
import so.shanku.essential.app.MyApplication;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class AttentionGroupActivity extends BaseActivity implements OnItemClickListener {
	@ViewInject(id = R.id.cancel_btn, click = "cancel_btn")
	Button cancel_btn;
	@ViewInject(id = R.id.new_group, click = "new_group")
	TextView new_group;
	private ListView mListView;
	private List<Map<String, String>> list;
	private ListViewAdapter mListViewAdapter;
	private PopupWindows popupWindows;
	/**
	 * @author FangDongzhang
	 * 
	 *         下面两行是模拟数据。
	 */
	private String[] itemName = { "张三", "李四", "王二" };

	/**
	 * @author FangDongzhang 2016/3/29 关注分组页面 1.选择分组 2.添加新分组
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_dialogactivity);

		Window window = getWindow();

		WindowManager.LayoutParams layoutParams = window.getAttributes();

		// 设置窗口的大小及透明度
		layoutParams.width = MyApplication.getInstance().getScreenWidth()
				- MyApplication.getInstance().getScreenWidth() / 4;

		layoutParams.height = MyApplication.getInstance().getScreenHeight()
				- MyApplication.getInstance().getScreenHeight() / 3;

		// layoutParams.alpha = 0.5f;

		window.setAttributes(layoutParams);

		list = new ArrayList<Map<String, String>>();
		for (int i = 0; i < 3; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("Name", itemName[i]);
			list.add(map);
		}

		mListView = (ListView) this.findViewById(R.id.listView);
		mListViewAdapter = new ListViewAdapter(this, list);
		mListView.setAdapter(mListViewAdapter);
		mListView.setOnItemClickListener(this);

		// 自定义回调函数
		mListViewAdapter.setOncheckChanged(new OnMyCheckChangedListener() {

			@Override
			public void setSelectID(int selectID) {
				mListViewAdapter.setSelectID(selectID); // 选中位置
				mListViewAdapter.notifyDataSetChanged(); // 刷新适配器
			}
		});
		TextView new_group = (TextView) findViewById(R.id.new_group);
		new_group.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popupWindows = new PopupWindows(mContext, v);
			}
		});
		
	}

	/**
	 * 取消
	 * 
	 * @param v
	 */
	public void cancel_btn(View v) {
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		mListViewAdapter.setSelectID(position); // 选中位置
		mListViewAdapter.notifyDataSetChanged(); // 刷新适配器
	}
	
//	/**
//	 * 新建分组
//	 * 
//	 * @param v
//	 */
//	public void new_group(View v) {
//		popupWindows = new PopupWindows(mContext, v);
//	}

	/**
	 * 新建分组
	 * 
	 * @author FangDongzhang
	 *
	 */
	public class PopupWindows extends PopupWindow {

		@SuppressWarnings("deprecation")
		public PopupWindows(Context mContext, View parent) {

			View view = View.inflate(mContext, R.layout.item_popupwindows, null);
			view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_ins));
			LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
			ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_bottom_in_2));

			setWidth(LayoutParams.FILL_PARENT);
			setHeight(LayoutParams.FILL_PARENT);
			setBackgroundDrawable(new BitmapDrawable());
			setOutsideTouchable(true);
			setFocusable(true);
			setOutsideTouchable(true);
			setContentView(view);
			showAtLocation(parent, Gravity.BOTTOM, 0, 0);
			update();

			Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);
			Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);
			Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);

			bt1.setText(getResources().getString(R.string.save_img));
			bt2.setText(getResources().getString(R.string.transmit));
			bt1.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// photo();
					dismiss();
				}
			});
			bt2.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// Intent intent = new Intent(PublishedActivity.this,
					// PicActivity.class);
					// startActivity(intent);
					dismiss();
				}
			});
			bt3.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					dismiss();
				}
			});

		}
	}
}
